<script setup>
import api from "./api.js";
import SearchBar from "./SearchBar.vue";
import tableCommon from "@/utils/tableCommon.js";
import RightToolbar from "@/components/RightToolbar/index.vue";
import { getCurrentInstance } from 'vue';

const { proxy } = getCurrentInstance();
const tableData = ref([]);
const tableRef = ref();

function getList() {
  api.list().then(response => {
    tableData.value = response;
  }).catch(error => {
    console.log(error);
  });
}

function Refresh() {
  proxy.$message.success("刷新成功");
}

/** 导出按钮操作 */
function handleExport() {
  api.export().then(response => {
    tableCommon.exportExcel(response);
  }).catch((e) => {
    console.error(e);
  });
}

onMounted(() => {
  getList();
});
</script>

<template>
  <div class="page-container">

    <div class="topbar-operate flexBetweenCenter">
      <div class="leftBox"></div>
      <search-bar :form="queryParams" @sub="getList" class="seach"></search-bar>
      <right-toolbar @query-table="Refresh" @export-table="handleExport"></right-toolbar>
    </div>

    <div class="table-container">
      <vxe-table border ref="tableRef" :column-config="{ resizable: true }"
        :tree-config="{ transform: true, rowField: 'id', parentField: 'parentId' }" :data="tableData"
        class="custom-table" @row-hover="handleRowHover">
        <vxe-column field="name" title="分类" tree-node></vxe-column>
        <vxe-column field="count" title="总数"></vxe-column>
        <vxe-column field="buyPrice" title="购置金额"></vxe-column>
      </vxe-table>
    </div>
  </div>
</template>

<style scoped lang="scss">
.page-container {
  height: 100%;
  padding: 5px 5px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  background-color: #f7f9fc;

}

.table-container {
  flex-grow: 1;
  overflow: auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.custom-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-family: 'Arial', sans-serif;
  font-size: 14px;
}

.flexBetweenCenter {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

</style>
